package cn.iocoder.yudao.module.orderview.service.orderview;

import cn.hutool.core.lang.UUID;
import cn.iocoder.yudao.module.orderview.controller.admin.vo.orderview.OrderViewRespVO;
import cn.iocoder.yudao.module.orderview.convert.orderview.OrderViewConvert;
import cn.iocoder.yudao.module.orderview.dal.redis.orderview.OrderViewRedisDAO;
import cn.iocoder.yudao.module.orderview.dal.redis.orderview.snapshot.OrderViewAggregationSnapshot;
import cn.iocoder.yudao.module.orderview.mq.producer.OrderViewTaskProducer;
import cn.iocoder.yudao.module.product.api.product.ProductApi;
import cn.iocoder.yudao.module.product.api.product.dto.ProductRespDTO;
import cn.iocoder.yudao.module.promotion.api.promotion.PromotionApi;
import cn.iocoder.yudao.module.promotion.api.promotion.dto.PromotionRespDTO;
import cn.iocoder.yudao.module.stock.api.stock.StockApi;
import cn.iocoder.yudao.module.stock.api.stock.dto.StockRespDTO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.module.orderview.dal.redis.RedisKeyConstants.ORDER_VIEW_TASK_PREFIX;
import static org.reflections.Reflections.log;

/**
 * 订单视图 Service 实现类
 *
 * @author 徐煜东
 */
@Service
@Validated
public class OrderViewServiceImpl implements OrderViewService {
    @Resource
    ProductApi productApi;
    @Resource
    StockApi stockApi;
    @Resource
    PromotionApi promotionApi;
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    OrderViewTaskProducer orderViewTaskProducer;
    @Resource
    OrderViewRedisDAO orderViewRedisDAO;

    @Override
    public String triggerOrderViewOnAsynTask(Long productId) {
        // 生成唯一任务 ID
        String requestId = UUID.randomUUID().toString();
        // 任务redisKey
        String redisKey = formatKey(String.valueOf(requestId));
        //判断是否有热点数据
        OrderViewAggregationSnapshot orderViewRedisSnapshot = orderViewRedisDAO.get0(String.valueOf(productId));
        if (orderViewRedisSnapshot != null) {
            // 结束任务
            log.info("[OrderViewServiceImpl][热点数据已存在结束视图任务 redisKey({})]", redisKey);
            redisTemplate.opsForValue().set(redisKey, "completed", 5, TimeUnit.MINUTES);
            // 将订单视图快照存储到新的request 中
            orderViewRedisSnapshot.setRequestId(requestId);
            orderViewRedisDAO.set(orderViewRedisSnapshot);
            return requestId;
        }
        log.info("[OrderViewServiceImpl][触发订单视图任务 redisKey({})]", redisKey);
        redisTemplate.opsForValue().set(redisKey, "processing", 5, TimeUnit.MINUTES);
        //发送 MQ 消息 异步查询
        orderViewTaskProducer.sendOrderViewTaskMessage(requestId, productId);
        return requestId;
    }

    @Override
    public OrderViewRespVO getOrderViewByProductIDOnSync(Long productId) {
        ProductRespDTO productRespDTO = productApi.getProduct(productId).getCheckedData();
        StockRespDTO stockRespDTO = stockApi.getStockByProuctId(productId).getCheckedData();
        PromotionRespDTO promotionRespDTO = promotionApi.getPromotionByProductId(productId).getCheckedData();
        return OrderViewConvert.INSTANCE.convert(productRespDTO, stockRespDTO, promotionRespDTO);
    }

    private static String formatKey(String requestId) {
        return String.format(ORDER_VIEW_TASK_PREFIX, requestId);
    }
}
